package network.udp.demo;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-26 15:49
 **/
public class UdpSend {
    public static void main(String[] args) {
        send();
    }

    private static void send() {
        Scanner scanner = null;
        DatagramSocket send = null;
        try {
            //    UDP发送数据：数据来自于键盘录入，直到输入的数据是886，发送数据结束
            scanner = new Scanner(System.in);
            //发送端socket
            send = new DatagramSocket();
            //获取发送数据
            String next;

            while (true) {
                next = scanner.next();
                DatagramPacket packet = new DatagramPacket(next.getBytes(), next.length()
                        , InetAddress.getByName("127.0.0.1"), 9000);
                send.send(packet);
                if (next.equals("886")) {
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (send != null) {

                send.close();
            }
            if (scanner != null) {

                scanner.close();
            }
        }
    }
}
